import 'package:gold/base/view_model.dart';
import 'package:gold/http/api_gold.dart';
import 'package:gold/http/http.dart';
import 'package:gold/model/goods_list_model.dart';
import 'package:gold/model/list_model.dart';
import 'package:gold/model/points_category_model.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';

/// @description:
/// @author tangzhentao
/// @created at 21:39 2024/11/25

class IntegralMallViewmodel extends Viewmodel {
  /// 刷新控制器
  final RefreshController controller = RefreshController();

  /// 数据列表
  final List<GoodsListModel> goodsList = [];

  /// 页码
  int _page = 1;

  /// 分类
  final List<PointsCategoryModel> categoryList = [];

  int _selectIndex = 0;
  int get selectIndex => _selectIndex;
  set selectIndex(int value) {
    if (_selectIndex != value) {
      _selectIndex = value;
      controller.requestRefresh();
    }
  }

  /// 加载数据
  Future loadFirst() async {
    setLoading();
    _page = 1;
    final categoryResult = await Http().requestList<PointsCategoryModel>(ApiGold.pointsCategory, showErrorMsg: false);
    if (categoryResult.success) {
      final list = categoryResult.data??[];
      list.removeWhere((e) => e.title == null || e.title?.isEmpty == true);
      categoryList.clear();
      categoryList.addAll(list);
      if (categoryList.isNotEmpty) {
        final result = await Http().request<ListModel<GoodsListModel>>(ApiGold.pointsList,
            data: {'page': _page, 'filter': {'category_id': categoryList[_selectIndex].id}, 'keywords': ''});
        if (result.success) {
          goodsList.clear();
          goodsList.addAll(result.data?.list?? []);
          if (goodsList.isNotEmpty) {
            setSuccess();
            controller.headerMode?.value = RefreshStatus.canRefresh;
            controller.footerMode?.value = _page < (result.data?.lastPage?? 0) ? LoadStatus.canLoading:LoadStatus.noMore;
          } else {
            setEmpty();
          }
        } else {
          setFailed(msg: result.msg);
        }
      } else {
        setEmpty(msg: '分类数据为空');
      }
    } else {
      setFailed(msg: categoryResult.msg);
    }
  }

  /// 刷新数据
  Future refresh() async {
    _page = 1;
    final result = await Http().request<ListModel<GoodsListModel>>(ApiGold.pointsList,
        data: {'page': _page, 'filter': {'category_id': categoryList[_selectIndex].id}, 'keywords': ''});
    if (result.success) {
      controller.refreshCompleted();
      controller.footerMode?.value = _page < (result.data?.lastPage?? 0) ? LoadStatus.canLoading:LoadStatus.noMore;
      final data = result.data?.list?? [];
      if (data.isNotEmpty) {
        goodsList.clear();
        goodsList.addAll(data);
        setSuccessChange();
      }
    } else {
      controller.refreshFailed();
    }
  }

  /// 加载更多数据
  Future loadMore() async {
    _page++;
    final result = await Http().request<ListModel<GoodsListModel>>(ApiGold.pointsList,
        data: {'page': _page, 'filter': {'category_id': categoryList[_selectIndex].id}, 'keywords': ''});
    if (result.success) {
      controller.loadComplete();
      controller.footerMode?.value = _page < (result.data?.lastPage?? 0) ? LoadStatus.canLoading:LoadStatus.noMore;
      final data = result.data?.list?? [];
      if (data.isNotEmpty) {
        goodsList.addAll(data);
        setSuccessChange();
      } else {
        controller.loadNoData();
      }
    } else {
      controller.loadFailed();
    }
  }

  @override
  onReload() => loadFirst();

  @override
  onRetry() => loadFirst();

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }
}
